package com.study.demo.aop.demo5;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

/**
 * invokeBeanFactoryPostProcessors:
 * 	    1. BeanDefinitionRegistryPostProcessor
 * 	        -> 1. postProcessBeanDefinitionRegistry()
 * 	        -> 2. postProcessBeanFactory()
 * 	    2. BeanFactoryPostProcessor
 * 	        -> postProcessBeanFactory()
 * @author: elvin
 * @time: 2019-08-15 15:55
 * @desc:
 **/
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println("1.当前容器中注册的bean个数: " + registry.getBeanDefinitionCount());
        System.out.println(String.join("," , registry.getBeanDefinitionNames()));
        registry.registerBeanDefinition("abcd", BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition());
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("2.当前容器中注册的bean个数: " + beanFactory.getBeanDefinitionCount());
        System.out.println(String.join("," , beanFactory.getBeanDefinitionNames()));
    }

}
